﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;


namespace BoardGame
{
    public enum Player { White, Black }


    public class Square
    {
        protected Square () { }

        static public readonly Square Empty = new Square ();

        public bool IsOccupied
        { get { return this.GetType () != typeof (Square); } }

        public virtual bool IsOpponent (Player player) { return false; }
        public virtual bool NotPlayer (Player player) { return true; }
        public virtual bool IsPlayer (Player player) { return false; }

        public override string ToString () { return "."; }
    }


    /// <summary>Represent the board as a two-dimensional array.</summary>
    /// <remarks>
    /// The board consists of a two-dimensional array of Squares. Each Square
    /// may be empty (a Square) or may be an instance of a subclass of Piece.
    /// </remarks>
    public class Board
    {
        internal Square[,] squares;

        // Soft-code the dimensions of the board.
        public int XSize { get { return squares.GetUpperBound (0) + 1; } }
        public int YSize { get { return squares.GetUpperBound (1) + 1; } }

        public Square this[int x, int y]
        { get { return squares[x, y]; } }


        /// <summary>Create a new board.</summary>
        /// <remarks>This constructor contains the only hard-coded size of the board.</remarks>
        internal Board ()
        {
            squares = new Square[8, 8];
            Clear ();
        }


        internal void Clear ()
        {
            for (int x = 0; x < XSize; ++x)
                for (int y = 0; y < YSize; ++y)
                    squares[x, y] = Square.Empty;
        }


        /// <summary>Produce a text representation of the current board.</summary>
        /// <remarks>The text representation of the board is useful for bench
        /// testing and is not by any GUI.</remarks>
        /// <returns>The current board in text form.</returns>
        public override string ToString ()
        {
            var result = new StringBuilder (XSize * 2 * YSize);

            for (int y = YSize - 1; ; --y)
            {
                for (int x = 0; ; ++x)
                {
                    result.Append (squares[x, y].ToString());

                    if (x >= XSize - 1)
                        break;

                    result.Append (' ');
                }

                if (y <= 0)
                    return result.ToString();

                result.Append ('\n');
            }
        }
    }
}
